package servlet;

import dao.UserDao;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "UserListServlet", value = "/UserListServlet.s")
public class UserListServlet extends HttpServlet {
    UserDao dao = new UserDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String uname = request.getParameter("uname");
        String status = request.getParameter("status");
        // 获取当前显示条数
        String pageSize = request.getParameter("pageSize");
        //获取当前页码
        String currentPage = request.getParameter("currentPage");
        System.out.println(currentPage);
        //把字符串转为int类型
        int size = Integer.valueOf(pageSize); //size表示每页多条数据
        int page = Integer.valueOf(currentPage);
        //分页查询条件 begin表示从表中第几条数据开始查 size 表示每页多少条数据
        int begin = size * (page - 1);

        //查询总行数total
        long total = dao.queryTotal(uname,status);

        //查询用户数据 data
        // 分页查询
        List<Map<String, Object>> data = dao.queryData(begin,size,uname,status);

        Map<String,Object> result = new HashMap<>();
        result.put("data",data);
        result.put("total",total);
        String json = new Gson().toJson(result);
        response.getWriter().print(json);
    }
}
